//
//  HMTabBar.swift
//  webo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {


    var composeColsure: (() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(composeBtn)

        self.composeBtn.addTarget(self, action: #selector(clickCompose), for: .touchUpInside)
        
    }
    
   @objc private func clickCompose(){
        
        composeColsure?()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    
    //不希望外界能够访问按钮的点击事件 需要设置为私有的
    //一旦添加了private 这个时候私有的方法 就对OC的运行循环不可见
    //@objc 是一个OC的标识
    //swift 会对继承自NSObject的对象的方法或者属性默认添加 @objc标识,不会对私有的属性或者方法添加@objc标识
    //添加@objc 是告诉系统这个方法或者属性具备OC特性(消息机制,KVC的动态派发)

    
    override func layoutSubviews() {
        super.layoutSubviews()
        //print(subviews)
        //需要获取 UITabBarButton类型的控件 并且修改frame
        //遍历子视图
        let w = UIScreen.main.bounds.size.width/5
        let h = self.bounds.size.height
        var index = 0
        
        for subview in subviews {
            
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRect(x: w * CGFloat(index), y: 0, width: w, height: h)
                index += (index == 1 ? 2 : 1)
            }
            
        }
        
        composeBtn.bounds.size = CGSize(width: w, height: h)
        composeBtn.center = CGPoint(x: self.center.x, y: h * 0.5)
        
    }
    
    lazy var composeBtn: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
    
        return btn
    }()
    
}
